<?php

class Filters extends CI_Controller
{
        public function __construct()
        {
                parent::__construct();
                $this->load->helper('url');
                $this->load->library('session');
                $this->load->library('FocusLib');
        }

        public function view()
        {
                // Check for session
                if(!$this->session->userdata('logged_in'))
                {
                        // Not signed in - show login page
                        header('Location: /auth/signin');
                }
                
                // User profile
                $user = unserialize( $this->session->userdata('profile') );

                // Title
                $data['title'] = 'Avaleht';

                // Main navigation
                $data['main_navigation'] = $this->load->view('main-navigation', array('logged_in' => TRUE), true);
                
                // Model
                $this->load->model('filters_model', 'filters');
                
                // Filters
                $filter_objects = $this->filters->get_by_user_id($user->ID);
                
                if($filter_objects)
                {
                    foreach ($filter_objects as $filter)
                    {
                        $data['filters'] []= $this->focuslib->buildClientFilter($filter);
                    }
                }
                else
                {
                    $data['filters'] = array();
                }
                
                $data['providers'] = $this->focuslib->listProviderTitles();

                $this->load->view('templates/header', $data);
                $this->load->view('content', $data);
                $this->load->view('templates/footer', $data);
        }

        public function add()
        {
                // Check for session
                if(!$this->session->userdata('logged_in'))
                {
                        // Not signed in - show login page
                        header('Location: /auth/signin');
                }

                // Title
                $data['title'] = 'Uue sisuallikajälgija lisamine';

                // Main navigation
                $data['main_navigation'] = $this->load->view('main-navigation', array('logged_in' => TRUE), true);

                // Providers
                $data['providers'] = $this->focuslib->listProviders();

                $this->load->view('templates/header', $data);
                $this->load->view('add-filter', $data);
                $this->load->view('templates/footer', $data);
        }
        
        public function edit($id)
        {
                // Check for session
                if(!$this->session->userdata('logged_in'))
                {
                        // Not signed in - show login page
                        header('Location: /auth/signin');
                }

                // Title
                $data['title'] = 'Sisuallikajälgija muutmine';

                // Main navigation
                $data['main_navigation'] = $this->load->view('main-navigation', array('logged_in' => TRUE), true);

                // Providers
                $data['providers'] = $this->focuslib->listProviders();
                
                // Model
                $this->load->model('filters_model', 'filters');
                
                // Filter
                $filter = $this->filters->get($id);
                if($filter)
                {
                    $filter->filterData = json_decode($filter->filterData, true);
                    $data['filter'] = $filter;
                }
                else
                {
                    // Invalid id
                    header('Location: /home');
                }

                $this->load->view('templates/header', $data);
                $this->load->view('edit-filter', $data);
                $this->load->view('templates/footer', $data);
        }
}

?>